﻿// =================================================================== 
// Module说明
//====================================================================
// 创建时间：2018-06-08 16:43:26
// 负责人：Sushee
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq.Expressions;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using OF.Component.Factory;
using OF.Component.DTO;
using System.Linq;
using OF.Component.Base;

namespace OF.Component.BLL
{

    /// <summary>
    ///发单动态 扩展
    /// </summary>
    public partial class PostSendOperate
    {
        /// <summary>
    	/// 根据断言条件获取单个发单动态
    	/// </summary>
        public static PostSendInfo QuerySingle(Expression<Func<PostSendInfo, bool>> predicate)
        {
            return dal.QuerySingle(predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取发单动态列表
    	/// </summary>
        public static IList<PostSendInfo> QueryList(Expression<Func<PostSendInfo, bool>> predicate)
        {
            return dal.QueryList(predicate);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="scObj"></param>
        /// <returns></returns>
        public static IList<PostSendDto> SearchList(scPostSend scObj)
        {
            return dal.SearchList(scObj);
        }

        /// <summary>
        /// 发单接单合并数据
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public static List<CarCircleInfoDto> SearchPostList(int shopId, int pageIndex, int pageSize, out int totalRecord)
        {
            totalRecord = 0;
            List<CarCircleInfoDto> postList = new List<CarCircleInfoDto>();
            CarCircleInfoDto obj = null;
            IList<PostInfoDto> list = dal.SearchPostList(shopId, pageIndex, pageSize, out totalRecord);
            foreach (var item in list)
            {
                var imgList = PostImgOperate.QueryList(a => a.PostID == item.PostID && a.PostType == item.PostType);
                obj = new CarCircleInfoDto
                {
                    ImageCount = imgList.Count,
                    ImageUrlList = imgList.Select(m => GetHttpFilePath(m.ImgUrl)).ToList(),
                    PostTitle = item.Content,
                    ShowTime = GetShowTime(item.CreatedDate),
                     PostType = item.PostType,
                     PostId = item.PostID
                };
                postList.Add(obj);
            }
            return postList;
        }

        private static string GetHttpFilePath(string relativePath)
        {
            return AppSettings.UploadFileHttpHost + relativePath;
        }

        private static List<string> GetShowTime(DateTime dt)
        {
            List<string> list = new List<string>();
            DateTime nowDt = DateTime.Now;
            var tSpan = nowDt.Date - dt.Date;
            if (tSpan.Days == 0)
            {
                list.Add("今天");
            }
            if (tSpan.Days == 1)
            {
                list.Add("昨天");
            }
            if (tSpan.Days > 1)
            {
                list.Add(dt.Date.ToString("dd"));
                list.Add(dt.Date.ToString("MM"));
            }
            return list;
        }

        /// <summary>
        /// 获取发单详情
        /// </summary>
        /// <param name="scObj"></param>
        /// <returns></returns>
        public static PostDetailDto SearchPostSendDetail(int postId)
        {
            return dal.SearchPostSendDetail(postId);
        }
    }
}